//
//  IGTopicImageCell.m
//  jingdongfang
//
//  Created by BOE on 2017/12/14.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicImageCell.h"
#import "JGGView.h"
#import "IGTopicModel.h"

static NSString *const kIGTopicImageCellID = @"kIGTopicImageCellID";

@interface IGTopicImageCell()

@property (nonatomic, strong) JGGView *jggView;

@end

@implementation IGTopicImageCell

+ (IGTopicImageCell *)cellWithTableView:(UITableView *)tableView {
    IGTopicImageCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGTopicImageCellID];
    if (!cell) {
        cell = [[IGTopicImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGTopicImageCellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor c3Color];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    self.jggView = [JGGView new];
    [self.contentView addSubview:self.jggView];
    [self.jggView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
    }];
}

- (void)setFeedDetail:(IGTopicModel *)feedDetail {
    _feedDetail = feedDetail;
    
    CGSize size = [IGTopicImageCell cellSize:feedDetail];
    ///解决图片复用问题
    [self.jggView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    ///布局九宫格
    [self.jggView JGGView:self.jggView dataSource:feedDetail.imgsBig smallDataSource:feedDetail.imgs completeBlock:^(NSInteger index, NSArray *dataSource,NSIndexPath *indexpath) {
//        self.tapImageBlock(index,dataSource,self.indexPath);
    }];
    
    [self.jggView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kGAP15);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(size.width, size.height));
    }];
}

+ (CGFloat)cellHeight:(IGTopicModel *)topicModel {
    CGSize size = [IGTopicImageCell cellSize:topicModel];
    return size.height + kGAP15;
}

+ (CGSize)cellSize:(IGTopicModel *)topicModel {
    CGFloat jjg_height = 0.0;
    CGFloat jjg_width = 0.0;
    BOOL jjg_exist = YES;
    if (topicModel.imgs.count > 0 && topicModel.imgs.count <= 3) {
        jjg_height = [JGGView imageHeight];
        jjg_width  = (topicModel.imgs.count)*([JGGView imageWidth]+kJGG_GAP)-kJGG_GAP;
    } else if (topicModel.imgs.count > 3 && topicModel.imgs.count <= 6){
        jjg_height = 2*([JGGView imageHeight] + kJGG_GAP) - kJGG_GAP;
        jjg_width  = 3*([JGGView imageWidth] + kJGG_GAP) - kJGG_GAP;
    } else if (topicModel.imgs.count >6 && topicModel.imgs.count <= 9){
        jjg_height = 3*([JGGView imageHeight] + kJGG_GAP) - kJGG_GAP;
        jjg_width  = 3*([JGGView imageWidth] + kJGG_GAP) - kJGG_GAP;
    } else {
        jjg_exist = NO;
    }

    return CGSizeMake(jjg_width, jjg_height);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
